home *** CD-ROM | disk | FTP | other *** search
/ Robotics & Artificial Int…3 (Professional Edition) / Robotics & Artificial Intelligence Tools 2003 (Professional Edition).iso / neural network tool and application / nsinstall.exe / data1.cab / Demos_Files / DLL / SangersShared.c < prev    next >
Encoding:
C/C++ Source or Header  |  2002-03-08  |  3.2 KB  |  102 lines

  1. // Dynamic link library implementation of NeuroSolutions SangersFull component 
  2.  
  3. #include "NSDLL.h" 
  4.  
  5. /*************************************************************/
  6. /* Macros to access the PE layers and weights in matrix form */
  7.  
  8. #define in(i,j)        input[j+i*inCols]
  9. #define out(i,j)    output[j+i*inCols]
  10. #define W(i,j)        weights[j+i*inCount]
  11. #define Wt(i,j)        weightsTranspose[i+j*outCount]
  12.  
  13. float *weightsTranspose=NULL;
  14. int    sangerInCountCheck=NULL,
  15.     sangerOutCountCheck=NULL,
  16.     transposeInCountCheck=NULL,
  17.     transposeOutCountCheck=NULL;
  18.  
  19. /******************************/
  20. /* Unsupervised weight update */
  21.  
  22. __declspec(dllexport) void performUnsupervised(
  23.     DLLData    *instance,    // Pointer to instance data (may be NULL)
  24.     NSFloat    *input,        // Pointer to input layer 
  25.     int     inRows,        // Number of rows of PEs in the input layer
  26.     int     inCols,        // Number of columns of PEs in the input layer
  27.     NSFloat    *output,     // Pointer to the output layer
  28.     int     outRows,    // Number of rows of PEs in the output layer
  29.     int     outCols,    // Number of columns of PEs in the output layer
  30.     NSFloat    *weights,    // Pointer to fully connected weight matrix 
  31.     NSFloat    step         // Learning rate
  32.     )
  33. {
  34.     int    i, j,
  35.         inCount=inRows*inCols,
  36.         outCount=outRows*outCols;
  37.     NSFloat partialSum;
  38.     
  39.     sangerInCountCheck = inCount;
  40.     sangerOutCountCheck = outCount;
  41.  
  42.     if ( (sangerInCountCheck==transposeOutCountCheck) && (sangerOutCountCheck==transposeInCountCheck) )
  43.     {
  44.     for (j=0; j<inCount; j++) {
  45.         partialSum = (NSFloat)0;
  46.         for (i=0; i<outCount; i++) {
  47.             partialSum += output[i] * W(i,j);
  48.             W(i,j) += step*output[i]*(input[j] - partialSum);
  49.                 Wt(i,j) = W(i,j);
  50.         }
  51.     }
  52.     }
  53. }
  54.  
  55. __declspec(dllexport) void performFullSynapse(
  56.     DLLData *instance,    // Pointer to instance data (may be NULL)
  57.     NSFloat    *input,     // Pointer to the input layer of processing elements (PEs)
  58.     int     inRows,        // Number of rows of PEs in the input layer
  59.     int     inCols,        // Number of columns of PEs in the input layer
  60.     NSFloat    *output,     // Pointer to the output layer
  61.     int     outRows,    // Number of rows of PEs in the output layer
  62.     int     outCols,    // Number of columns of PEs in the output layer
  63.     NSFloat    *weights     // Pointer to the fully connected matrix of weights
  64.     )
  65. {
  66.     int    i, j,
  67.         inCount=inRows*inCols,
  68.         outCount=outRows*outCols;
  69.     
  70.     transposeInCountCheck = inCount;
  71.     transposeOutCountCheck = outCount;
  72.     
  73.     if ( (sangerInCountCheck==transposeOutCountCheck) && (sangerOutCountCheck==transposeInCountCheck) )
  74.     {
  75.         weightsTranspose = weights;            
  76.         for (i=0; i<outCount; i++)
  77.             for (j=0; j<inCount; j++)
  78.                 output[i] += W(i,j)*input[j];
  79.     }
  80. }
  81.  
  82. /******************************************/
  83. /* Management of instance data (OPTIONAL) */
  84. /*
  85. __declspec(dllexport) DLLData *allocUnsupervised(
  86.     DLLData    *oldInstance,    // Pointer to the last instance if reallocating
  87.     int     inRows,        // Number of rows of PEs in the input layer
  88.     int     inCols,        // Number of columns of PEs in the input layer
  89.     int     outRows,    // Number of rows of PEs in the output layer
  90.     int     outCols        // Number of columns of PEs in the output layer
  91.     )
  92. {
  93.     DLLData *instance = allocDLLInstance(oldInstance);
  94.     return instance;
  95. }
  96.  
  97. __declspec(dllexport) void freeUnsupervised(DLLData *instance)
  98. {
  99.     freeDLLInstance(instance);
  100. }
  101. */
  102.